#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Flask项目的简单分页器
替代Django分页器，解决兼容性问题
"""

class SimplePaginator:
    """简单的分页器，替代Django分页器"""
    
    def __init__(self, data, page_size):
        """
        初始化分页器
        :param data: 要分页的数据列表
        :param page_size: 每页显示的数据量
        """
        self.data = data if data else []
        self.page_size = int(page_size) if page_size and str(page_size).isdigit() else 10
        self.total_count = len(self.data)
        self.total_pages = (self.total_count + self.page_size - 1) // self.page_size if self.page_size > 0 else 1
    
    def page(self, page_index):
        """
        获取指定页的数据
        :param page_index: 页码（从1开始）
        :return: 当前页的数据列表
        """
        try:
            page_index = int(page_index) if page_index and str(page_index).isdigit() else 1
            page_index = max(1, min(page_index, self.total_pages))  # 确保页码在有效范围内
            
            start_index = (page_index - 1) * self.page_size
            end_index = start_index + self.page_size
            
            return self.data[start_index:end_index]
        except (ValueError, TypeError):
            return self.data[:self.page_size] if self.data else []
    
    def get_page_info(self, page_index):
        """
        获取分页信息
        :param page_index: 当前页码
        :return: 包含分页信息的字典
        """
        try:
            page_index = int(page_index) if page_index and str(page_index).isdigit() else 1
            page_index = max(1, min(page_index, self.total_pages))
            
            return {
                'current_page': page_index,
                'total_pages': self.total_pages,
                'total_count': self.total_count,
                'page_size': self.page_size,
                'has_previous': page_index > 1,
                'has_next': page_index < self.total_pages,
                'previous_page': page_index - 1 if page_index > 1 else None,
                'next_page': page_index + 1 if page_index < self.total_pages else None
            }
        except (ValueError, TypeError):
            return {
                'current_page': 1,
                'total_pages': self.total_pages,
                'total_count': self.total_count,
                'page_size': self.page_size,
                'has_previous': False,
                'has_next': self.total_pages > 1,
                'previous_page': None,
                'next_page': 2 if self.total_pages > 1 else None
            }






